<?
ini_set("display_errors",1);
include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/portals/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');

init_session();

if (empty($_SESSION['user_id'])) {
	include($_SERVER['DOCUMENT_ROOT'].'/portals/login.inc.php');
	exit;
}



include($_SERVER['DOCUMENT_ROOT'].'/portals/header.inc.php');



$id = intval(@$_GET['id']);



if ($id) {
	$current = getRow("SELECT * FROM portal WHERE portal_id = $id");
	
	if ($current['status_id'] != 1 || $current['user_id'] != $_SESSION['user_id']) {
		die("unable to edit this Portal");
	}
	print "<h3>Reset Current Portal</h3>";
} else {
	die("huh?");
}

if (!empty($_POST)) {
		
		queryExecute("UPDATE `filter` SET last_run = 0 WHERE portal_id = $id"); //schedule it to run again. 
			
		$table = "p2.gridimage{$id}_bi";
		queryExecute("TRUNCATE TABLE $table"); //careful not to run this against the view!


		
		print "<p>Updates Saved. Thank you</p>";

		print "<p><a href=\"portal.php?id=$id\">View Portal Page</a></p>";

?>

<div style="background-color:pink;padding:10px">
	NOTE: It can take a few minutes for images to begin populating a new portal once the filters have been defined. In addition changing the filters for the moment wont update the images in the portal very quickly. See the 
<a href="faq.php">FAQ</a>.
</div>


<form name="redirect">
<p>You will be automatically redirected in <input type="text" size="3" name="seconds"> seconds.</p>
</form>
<script>
var destiniation="portal.php?id=<? echo $id; ?>";
var start=5;
var current=document.forms['redirect'].seconds.value=start+1
function redirectTick() {
	if (current==1) {
		window.location=destiniation;
		return;
	} else{
		current-=1;
		document.forms['redirect'].seconds.value=current;
		setTimeout("redirectTick()",1000);
	}	
}
redirectTick();
</script>
<?


		exit;
	

}



if ($id) {
	print "<p>Resetting Portal: <b>".he($current['title'])."</b>. <a href=\"portal.php?id=$id\">Return to listing</a> (without saving changes)</p>";
}

?>

<form method="post">

<p><b>Please confirm you wish to do this. All images will be removed from your portal.</b> All custom settings (such as sort order, and the date it was added to the portal) will be lost.</p>

<p>This can be useful if the portal was incorrectly populated, and you have now edited the filters. The current filters will be used again fresh, just like a brand new portal.</p>

<p>Because the images are removed and re-added according to the current filters, the 'Added Date' is reset on all images.</p>

<p> NOTE: It can take a few minutes for images to begin re-populating the portal.</p>

<div style="border-top:3px solid silver;margin-top:20px">
<input type="submit" name="save" value="I Confirm"/> <a href="portal.php?id=<? echo $id; ?>">Cancel</a>
</div>

</form>

<?

include($_SERVER['DOCUMENT_ROOT'].'/portals/footer.inc.php');
